﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Model;

namespace TanHungLong
{
    public partial class FrmLogin : RmsControl.FrmBase
    {
        #region variant
        private FrmMain main = null;
        public event OnCompletedEventHandler OnCompleted = null;
        public bool isLogin = false;
        #endregion variant

        //=====================================
        #region form
        public FrmLogin(FrmMain main)
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.main = main;
            this.main.ShowBgr();
            ShowForm(this.main);

            this.FormClosing += new FormClosingEventHandler(FrmLogin_FormClosing);
            this.Shown += new EventHandler(FrmLogin_Shown);

            this.btnLogin.Click += new EventHandler(btnLogin_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            Utility.InitControl(this.lookUpNhanVienID);

            DangNhapBOList dangNhapBOList = DangNhapBOList.GetDangNhapBOList("TinhTrangCongViecID<3 AND LoaiTaiKhoanID IS NOT NULL AND TenDangNhap IS NOT NULL AND MatKhau IS NOT NULL", "TenDangNhap");
            dangNhapBOList.InsertCustomValue(-1, "admin");
            Utility.SetData(this.lookUpNhanVienID, dangNhapBOList, dangNhapBOList.Count);
            this.lookUpNhanVienID.EditValue = -1;

            this.KeyDown += new KeyEventHandler(FrmLogin_KeyDown);
        }

        private void FrmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6))
                    Login();
                else
                    if ((e.KeyCode == Keys.E && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F2))
                        Login();
        }

        private void FrmLogin_Shown(object sender, EventArgs e)
        {
            this.lookUpNhanVienID.Focus();
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
            else
            {
                if (this.OnCompleted != null)
                {
                    OnCompletedEventArgs evt = new OnCompletedEventArgs(this.isLogin);
                    this.OnCompleted(this, evt);
                }

                this.main.Controls.Remove(this);
                this.main.HideBgr();
                this.Dispose();
            }
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.isLogin = false;
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function
        private void Login()
        {
            this.dxErrorProvider1.SetError(this.lookUpNhanVienID, "");
            this.dxErrorProvider1.SetError(this.txtMatKhau, "");

            Session.LoginID = StaticUtil.GetInt(this.lookUpNhanVienID.EditValue);

            ClTripleDES tripleDes = new ClTripleDES();
            string MatKhauCu = "";

            if (Session.LoginID == -1)
                MatKhauCu = tripleDes.Decrypte(Session.thietDatBOCollection.GetStrValue(LoaiThietDat.MatKhau));
            else
            {
                NhanVienDangNhapBO nhanVienBO = NhanVienDangNhapBO.GetRootNhanVienDangNhapBO(Session.LoginID);
                MatKhauCu = tripleDes.Decrypte(nhanVienBO.MatKhau);
            }

            if (this.txtMatKhau.Text != MatKhauCu)
            {
                this.dxErrorProvider1.SetError(this.txtMatKhau, "Mật khẩu không đúng");
                return;
            }

            Session.LoginPC = StaticUtil.LocalIPAddress() + " - " + System.Environment.MachineName;
            Session.LoginName = this.lookUpNhanVienID.Text.ToUpper();

            LogBO logBO = LogBO.NewRootLogBO();
            logBO.TacVu = "Đăng nhập";
            logBO.ObjectID = -1;
            logBO = logBO.Save();

            this.isLogin = true;
            this.Close();
        }
        #endregion function
    }
}